package com.gov.originOutput;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;

/**
 * txt原有内容输出到一个新文件
 *
 * @author ZhangDong
 * @date 2024/9/5 9:05
 */
public class OriginOutputUtil {

    private static final Logger LOG = LoggerFactory.getLogger(OriginOutputUtil.class);
    private static final String OUT_FILE_NAME = "output.txt";

    public static void originOutput(String[] args) {
        if (args.length <= 0) {
            return;
        }
        String arg = args[0];
        File inFile = new File(arg);
        if (!inFile.exists()) {
            return;
        }

        File parentFile = inFile.getParentFile();
        String parentFilePath = parentFile.getPath();
        LOG.info("parentFilePath：{}", parentFilePath);
        File outputFile = new File(parentFilePath + File.separator + OUT_FILE_NAME);
        try (FileWriter fileWriter = new FileWriter(outputFile, true);
             BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(inFile), "gbk"))) {
            String string;
            while ((string = br.readLine()) != null) {
                fileWriter.write(string);
                fileWriter.write("\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
